<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所在位置" prop="position">
        <el-select v-model="queryParams.position" placeholder="请选择" clearable>
          <el-option
            v-for="dict in positionList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option
            v-for="dict in typeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['information:recom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['information:recom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:recom:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recomList" @selection-change="handleSelectionChange" height="650" fixed>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="所在位置" align="center" prop="position" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="图片" align="center" prop="photo" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column width="220" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:recom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:recom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改推荐列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal='false' append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所在位置" prop="position">
          <el-select v-model="form.position" placeholder="请选择" clearable>
            <el-option
              v-for="dict in positionList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" @change="changeType" placeholder="请选择"  clearable>
            <el-option
              v-for="dict in typeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="recomId">
          <el-select v-model="form.recomId" placeholder="请选择" clearable>
            <el-option
              v-for="dict in selectList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listRecom, getRecom, delRecom, addRecom, updateRecom, listAllSpace} from "@/api/information/recom";

export default {
  name: "Recom",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 推荐列表表格数据
      recomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        position: null,
        type: null,
        recomId: null,
        name: null,
        photo: null,
        num: null
      },
      list: {
        parkList: [],
        buildingCommercialList: [],
        buildingOfficeList: [],
        warehouseList: [],
        workShopList: [],
        apartmentList: [],
        auctionList: [],
        landList: [],
        introList:[],
        companyNeedList:[],
        classroomList:[],
        articleList:[],
        serviceArticleList:[],
        wenzhangList:[]
      },
      selectList: [],
      positionList:[
        {
          value:"首页",
          label:"首页"
        },
        {
          value:"地域营商环境",
          label:"地域营商环境"
        },
        {
          value:"物理空间展示",
          label:"物理空间展示"
        },
        {
          value:"科技产业服务港",
          label:"科技产业服务港"
        },
        // {
        //   value:"亦朋科技产品体系",
        //   label:"亦朋科技产品体系"
        // },
        // {
        //   value:"企业需求服务",
        //   label:"企业需求服务"
        // },
        // {
        //   value:"云课堂",
        //   label:"云课堂"
        // },
        // {
        //   value:"服务案例",
        //   label:"服务案例"
        // },
        // {
        //   value:"资讯",
        //   label:"资讯"
        // },
      ],
      typeList:[
        {
          value:"区域营商环境介绍",
          label:"区域营商环境介绍"
        },
        {
          value:"园区",
          label:"园区"
        },
        {
          value:"楼宇商用",
          label:"楼宇商用"
        },
        {
          value:"楼宇办公",
          label:"楼宇办公"
        },
        {
          value:"拍卖",
          label:"拍卖"
        },
        {
          value:"土地",
          label:"土地"
        },
        {
          value:"仓库",
          label:"仓库"
        },
        {
          value:"公寓",
          label:"公寓"
        },
        {
          value:"厂房",
          label:"厂房"
        },
        {
          value:"服务资讯",
          label:"服务资讯"
        },
        {
          value:"企业需求",
          label:"企业需求"
        },
        {
          value:"云课堂",
          label:"云课堂"
        },
        {
          value:"服务案例",
          label:"服务案例"
        },
        {
          value:"资讯",
          label:"资讯"
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        position: [
          { required: true, message: "所在位置不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        recomId: [
          { required: true, message: "推荐不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        photo: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        num: [
          { required: true, message: "浏览量不能为空", trigger: "blur" }
        ]
      }
    };
  },

  created() {
    this.getList();
    this.getSpace();
  },
  methods: {
    getSpace() {
      listAllSpace().then(res => {
        this.list.parkList = res.park
        this.list.buildingCommercialList = res.buildingCommercial
        this.list.buildingOfficeList = res.buildingOffice
        this.list.landList = res.land
        this.list.warehouseList = res.warehouse
        this.list.workShopList = res.workShop
        this.list.apartmentList = res.apartment
        this.list.auctionList = res.auction
        this.list.introList = res.intro
        this.list.wenzhangList = res.wenzhang
        this.list.articleList = res.article
        this.list.serviceArticleList = res.serviceArticle
        this.list.classroomList = res.classroom
        this.list.companyNeedList = res.companyNeed
      });
    },
    changeType() {
      this.form.recomId = null
      switch (this.form.type) {
        case "园区" :
          this.selectList = this.list.parkList;
          break;
        case "楼宇商用" :
          this.selectList = this.list.buildingCommercialList;
          break;
        case "楼宇办公" :
          this.selectList = this.list.buildingOfficeList;
          break;
        case "仓库" :
          this.selectList = this.list.warehouseList;
          break;
        case "厂房" :
          this.selectList = this.list.workShopList;
          break;
        case "公寓" :
          this.selectList = this.list.apartmentList;
          break;
        case "拍卖" :
          this.selectList = this.list.auctionList;
          break;
        case "土地" :
          this.selectList = this.list.landList;
          break;
        case "企业需求" :
          this.selectList = this.list.companyNeedList;
          break;
        case "云课堂" :
          this.selectList = this.list.classroomList;
          break;
        case "区域营商环境介绍" :
          this.selectList = this.list.introList;
          break;
        case "资讯" :
          this.selectList = this.list.articleList;
          break;
        case "服务资讯" :
          this.selectList = this.list.serviceArticleList;
          break;
        case "服务案例" :
          this.selectList = this.list.wenzhangList;
          break;
      }
    },
    /** 查询推荐列表列表 */
    getList() {
      this.loading = true;
      listRecom(this.queryParams).then(response => {
        this.recomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        position: null,
        type: null,
        recomId: null,
        name: null,
        photo: null,
        num: null
      };
      this.selectList = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加推荐列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改推荐列表";
        switch (this.form.type) {
          case "园区" :
            this.selectList = this.list.parkList;
            break;
          case "楼宇商用" :
            this.selectList = this.list.buildingCommercialList;
            break;
          case "楼宇办公" :
            this.selectList = this.list.buildingOfficeList;
            break;
          case "仓库" :
            this.selectList = this.list.warehouseList;
            break;
          case "厂房" :
            this.selectList = this.list.workShopList;
            break;
          case "公寓" :
            this.selectList = this.list.apartmentList;
            break;
          case "拍卖" :
            this.selectList = this.list.auctionList;
            break;
          case "土地" :
            this.selectList = this.list.landList;
            break;
          case "企业需求" :
            this.selectList = this.list.companyNeedList;
            break;
          case "云课堂" :
            this.selectList = this.list.classroomList;
            break;
          case "区域营商环境介绍" :
            this.selectList = this.list.introList;
            break;
          case "资讯" :
            this.selectList = this.list.articleList;
            break;
          case "服务资讯" :
            this.selectList = this.list.serviceArticleList;
            break;
          case "服务案例" :
            this.selectList = this.list.wenzhangList;
            break;
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除推荐列表编号为"' + ids + '"的数据项？').then(function() {
        return delRecom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('information/recom/export', {
        ...this.queryParams
      }, `recom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
